<?php
// +----------------------------------------------------------------------
// | UCToo [ Universal Convergence Technology ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2020 https://www.uctoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Patrick <contact@uctoo.com>
// +----------------------------------------------------------------------

namespace app\wechatopen\behavior;

use addons\wechat\library\Wechat as WechatService;
use app\admin\model\wechatopen\WechatopenAutoreply;
use app\admin\model\wechatopen\WechatopenConfig;
use app\admin\model\wechatopen\WechatopenContext;
use app\admin\model\wechatopen\WechatopenResponse;
use app\admin\model\wechatopen\Applet;


class CustomSendAutoReply
{
    /**
     * 用户进入默认发图片
     * @access public
     */
    public function textAutoReply($message)
    {
        $appid = input('appid');
        $appletConfig = Applet::getAppletConfig($appid);
        $matches = null;
          $wechatService = new WechatService;
          $WechatopenConfig = new WechatopenConfig($appletConfig);
          $unknownMessage = (new WechatopenConfig($appletConfig))::getValue('default.unknown.message');
          $unknownMessage = $unknownMessage ? $unknownMessage : "";

          //自动回复处理
         if ($message['MsgType'] == 'text') {
              $autoreply = null;
              $autoreplyList = (new WechatopenAutoreply($appletConfig))::where('status', 'normal')->cache(true)->order('weigh DESC,id DESC')->select();
              foreach ($autoreplyList as $index => $item) {
                  //完全匹配和正则匹配
                  if ($item['text'] == $message['Content'] || (in_array(mb_substr($item['text'], 0, 1), ['#', '~', '/']) && preg_match($item['text'], $message['Content'], $matches))) {
                      $autoreply = $item;
                      break;
                  }
              }
              if ($autoreply) {
                  $wechatResponse = (new WechatopenResponse($appletConfig))::where(["eventkey" => $autoreply['eventkey'], 'status' => 'normal'])->find();
                  if ($wechatResponse) {
                      $responseContent = (array)json_decode($wechatResponse['content'], true);
                      $wechatContext = (new WechatopenContext($appletConfig))::where(['openid' => $message['FromUserName']])->order('id', 'desc')->find();
                      $result = $wechatService->response($this, $message['FromUserName'], $message['Content'], $responseContent, $wechatContext, $matches);
                      if ($result) {
                          return $result;
                      }
                  }
              }
          }
          return $unknownMessage;
    }

    /**
     * 用户关注默认发消息
     * @access public
     */
    public function run()
    {
    }
}
